﻿// From: http://www.codeproject.com/KB/WPF/PieChartDataBinding.aspx
// With small changes from Patrick Boos

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ScrumTable.UI.View.WinTouch.UserControls.PieChart.PieChart
{
    /// <summary>
    /// Defines the layout of the pie chart
    /// </summary>
    public partial class PieChartLayout : UserControl
    {
        #region dependency properties

        #region PlottedProperty
        /// <summary>
        /// The property of the bound object that will be plotted (CLR wrapper)
        /// </summary>
        public String PlottedProperty
        {
            get { return GetPlottedProperty(this); }
            set { SetPlottedProperty(this, value); }
        }

        // PlottedProperty dependency property
        public static readonly DependencyProperty PlottedPropertyProperty =
            DependencyProperty.RegisterAttached("PlottedProperty", typeof(String), typeof(PieChartLayout),
                                                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Inherits));

        // PlottedProperty attached property accessors
        public static void SetPlottedProperty(UIElement element, String value)
        {
            element.SetValue(PlottedPropertyProperty, value);
        }
        public static String GetPlottedProperty(UIElement element)
        {
            return (String)element.GetValue(PlottedPropertyProperty);
        }
        #endregion

        #region ColorProperty
        /// <summary>
        /// The property of the bound object that will be used as color (CLR wrapper)
        /// </summary>
        public String ColorProperty
        {
            get { return GetColorProperty(this); }
            set { SetColorProperty(this, value); }
        }

        // ColorProperty dependency property
        public static readonly DependencyProperty ColorPropertyProperty =
            DependencyProperty.RegisterAttached("ColorProperty", typeof(String), typeof(PieChartLayout),
                                                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Inherits));

        // ColorProperty attached property accessors
        public static void SetColorProperty(UIElement element, String value)
        {
            element.SetValue(ColorPropertyProperty, value);
        }
        public static String GetColorProperty(UIElement element)
        {
            return (String)element.GetValue(ColorPropertyProperty);
        }
        #endregion

        /// <summary>
        /// The property of the bound object that will be plotted (CLR wrapper)
        /// </summary>
        public String LegendTitle
        {
            get { return GetLegendTitle(this); }
            set { SetLegendTitle(this, value); }
        }

        // PlottedProperty dependency property
        public static readonly DependencyProperty LegendTitleProperty =
            DependencyProperty.RegisterAttached("LegendTitle", typeof(String), typeof(PieChartLayout),
                                                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Inherits));

        // PlottedProperty attached property accessors
        public static void SetLegendTitle(UIElement element, String value)
        {
            element.SetValue(LegendTitleProperty, value);
        }
        public static String GetLegendTitle(UIElement element)
        {
            return (String)element.GetValue(LegendTitleProperty);
        }

        /// <summary>
        /// A class which selects a color based on the item being rendered.
        /// </summary>
        public IColorSelector ColorSelector
        {
            get { return GetColorSelector(this); }
            set { SetColorSelector(this, value); }
        }

        // ColorSelector dependency property
        public static readonly DependencyProperty ColorSelectorProperty =
            DependencyProperty.RegisterAttached("ColorSelectorProperty", typeof(IColorSelector), typeof(PieChartLayout),
                                                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

        // ColorSelector attached property accessors
        public static void SetColorSelector(UIElement element, IColorSelector value)
        {
            element.SetValue(ColorSelectorProperty, value);
        }
        public static IColorSelector GetColorSelector(UIElement element)
        {
            var colors = (IColorSelector)element.GetValue(ColorSelectorProperty);

            if(colors==null)
                colors = MakeDefaultColors();

            return colors;
        }

        private static IColorSelector MakeDefaultColors()
        {
            return new IndexedColourSelector
                             {
                                 Brushes = new Brush[]
                                               {
                                                   new SolidColorBrush(Color.FromRgb(0x9F, 0x15, 0xC3)),
                                                   new SolidColorBrush(Color.FromRgb(0xFF, 0x8E, 0x01)),
                                                   new SolidColorBrush(Color.FromRgb(0x33, 0x99, 0x33)),
                                                   new SolidColorBrush(Color.FromRgb(0x00, 0xAA, 0xFF)),
                                                   new SolidColorBrush(Color.FromRgb(0x81, 0x81, 0x83)),
                                                   new SolidColorBrush(Color.FromRgb(0x00, 0x00, 0x33))
                                               }
                             };
        }


        #endregion

        public PieChartLayout()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeComponent();
            }
        }


        ~PieChartLayout()
        {
            Dispose(false);
        }


        /// <summary>
        /// Implement dispose interface in order to support runtime support.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources.
        /// </summary>
        /// <param name="disposing">True to dispose the managed resources, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            ThePiePlotter.Dispose();
            TheLegend.Dispose();
        }
    }
}